package com.example.springbootshiro.util;

import org.apache.shiro.crypto.hash.Md5Hash;

import java.util.Random;

/**
 * 密码处理
 * Created by YangGang on 2021/12/15 11:49。
 */
public class PasswordUtil {
    private static char[] chars = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM!@#$%^&*1234567890".toCharArray();
    private static int iterations = 1024;//散列次数

    /**
     * 对字符串进行 md5+salt 处理
     *
     * @param str
     * @return
     */
    public static String md5AndSalt(String str, String salt) {
        return new Md5Hash(str, salt, iterations).toHex();
    }

    /**
     * 随机生成几位数的盐
     *
     * @param num 位数
     * @return
     */
    public static String getSalt(int num) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num; i++) {
            sb.append(chars[new Random().nextInt(chars.length)]);
        }
        return sb.toString();
    }


//    public static void main(String[] args) {
//        System.out.println(getSalt(7));
//    }

}
